package com.cloudsigns.factorymanager2.project.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.activity.AddProductInfomationActivity;
import com.cloudsigns.factorymanager2.project.activity.EditProductInformationActivity;
import com.cloudsigns.factorymanager2.project.adapter.ProductInformationAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.customview.WorkSheetCategoryPoupWindow;
import com.cloudsigns.factorymanager2.project.customview.WorkSheetLevelPopupWindow;
import com.cloudsigns.factorymanager2.project.myinerface.ItemEditListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 编辑 生产工单
 * Created by tony on 2017/12/22.
 */

public class ProductionOrderEditFragment extends Fragment implements View.OnClickListener, ItemEditListener {
    private EditText edt__project_name;
    private ImageView img_add_product;
    private TextView edt__work_sheet_category, edt__level;
    private TextView btn_save;
    private WorkSheetCategoryPoupWindow workSheetPopupwindow;
    private WorkSheetLevelPopupWindow workSheetLevelPopupWindow;
    private String workSheetCategoryStr, worksheetLevelStr;
    private ListView lv;
    private ProductInformationAdapter adapter;
    private List<ProductBean> productBeanList = new ArrayList<>();
    private final String TAG = "ProductionOrderEditFragment";
    private int selectedPosition = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_production_order_edit, container, false);
        init(view);
        return view;
    }


    private void init(View view) {

        edt__work_sheet_category = view.findViewById(R.id.edt__work_sheet_category);
        edt__level = view.findViewById(R.id.edt__level);
        img_add_product = view.findViewById(R.id.img_add_product);
        btn_save = view.findViewById(R.id.btn_save);
        lv = view.findViewById(R.id.lv);


        edt__work_sheet_category.setOnClickListener(this);
        edt__level.setOnClickListener(this);
        img_add_product.setOnClickListener(this);
        btn_save.setOnClickListener(this);


        adapter = new ProductInformationAdapter(getActivity(), TAG);
        adapter.setItemEditListener(this);
        adapter.setData(productBeanList);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                selectedPosition = position;

                Intent mIntent = new Intent(getActivity(), EditProductInformationActivity.class);
                startActivityForResult(mIntent, ConstantUtil.EDIT_PRODUCT_INFOMATION);
            }
        });

    }

    private void showWorkSheetCategoryPoupWindow(View anchor) {
        workSheetPopupwindow = new WorkSheetCategoryPoupWindow(getActivity());
        workSheetPopupwindow.setOnClickListener(this);
        workSheetPopupwindow.showPopupWindow(anchor);
    }

    private void showLeverPopupWindow(View anchor) {
        workSheetLevelPopupWindow = new WorkSheetLevelPopupWindow(getActivity());
        workSheetLevelPopupWindow.setOnClickListener(this);
        workSheetLevelPopupWindow.showPopupWindow(anchor);
    }

    private void save() {
        Toast.makeText(getActivity(), "保存成功", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void edit(int positon) {
        selectedPosition = positon;

        Intent mIntent = new Intent(getActivity(), EditProductInformationActivity.class);
        startActivityForResult(mIntent, ConstantUtil.EDIT_PRODUCT_INFOMATION);
    }

    //    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        if (workSheetPopupwindow != null) {
//            workSheetPopupwindow.dismiss();
//            workSheetPopupwindow = null;
//        }
//
//        if (workSheetLevelPopupWindow != null) {
//            workSheetLevelPopupWindow.dismiss();
//            workSheetLevelPopupWindow = null;
//        }
//    }
//

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {

            case R.id.edt__work_sheet_category:
                showWorkSheetCategoryPoupWindow(view);
                break;
            case R.id.img_add_product:
                mIntent = new Intent(getActivity(), AddProductInfomationActivity.class);
                startActivityForResult(mIntent, ConstantUtil.ADD_PRODUCT_INFOMATION);
                break;
            case R.id.btn_save:
                save();
                break;
            case R.id.tv_A:
            case R.id.tv_B:
            case R.id.tv_C:
            case R.id.tv_D:

                tv = (TextView) view;
                workSheetCategoryStr = tv.getText().toString().trim();
                edt__work_sheet_category.setText(workSheetCategoryStr);
                if (workSheetPopupwindow != null)
                    workSheetPopupwindow.dismiss();
                break;
            case R.id.tv_first_level:
            case R.id.tv_second_lever:
            case R.id.tv_third_level:
            case R.id.tv_firth_lever:
                tv = (TextView) view;
                worksheetLevelStr = tv.getText().toString().trim();
                edt__level.setText(worksheetLevelStr);
                if (workSheetLevelPopupWindow != null)
                    workSheetLevelPopupWindow.dismiss();

                break;
            case R.id.edt__level:
                showLeverPopupWindow(view);
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.ADD_PRODUCT_INFOMATION: //添加产品信息
                if (data != null) {
                    boolean bool = data.getBooleanExtra(ConstantUtil.PRODUCTBEAN, false);
                    if (bool) {
//                        productBeanList.add(new ProductBean(null, 0, null, null, 0, null));
                        adapter.setData(productBeanList);
                        adapter.notifyDataSetChanged();
                    }
//                    ProductBean productBean = (ProductBean) data.getSerializableExtra(ConstantUtil.PRODUCTBEAN);
//                    productBeanList.add(productBean);
//                    adapter.notifyDataSetChanged();
                }
                break;
            case ConstantUtil.EDIT_PRODUCT_INFOMATION:  //编辑产品信息
                if (data != null) {
                    boolean bool = data.getBooleanExtra(ConstantUtil.PRODUCTBEAN, false);
                    if (bool) {
                        if (resultCode == ConstantUtil.DELETE_PRODUCT_INFOMATION && selectedPosition >= 0) {
                            productBeanList.remove(selectedPosition);
                            adapter.setData(productBeanList);
                            adapter.notifyDataSetChanged();

                            selectedPosition = -1;
                        }
                    }
                    break;
                }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}
